package hotel.board;

import hotel.board.circuit.ICase;
import hotel.board.circuit.ICircuit;
import hotel.board.circuit.predefined.IPredefinedCircuit;
import hotel.dice.BuildDiceFaces;
import hotel.hotel.IHotelsArea;
import hotel.hotel.IProperty;
import hotel.player.IPlayer;

public class PrintableBoard extends ExtendedBoard {
	
	// CONSTRUCTEUR

	public PrintableBoard(String[] names, IPredefinedCircuit c) {
		super(names, c);
	}
	
	// REQUETES
	
	public String toString() {
		StringBuffer res = new StringBuffer();
		
		// Dés
		Integer moveVal = getMoveDiceFace();
		BuildDiceFaces buildVal = getBuildDiceFace();
		res.append("Dé move : [" + (moveVal == null ? " " : moveVal) + "]"
				+ "     Dé build : [" + (buildVal == null ? " " : "" + buildVal.name().charAt(0)) + "]\n");
		
		res.append("\n");
		
		// Joueurs
		for (IPlayer pl : getPlayers()) {
			res.append(sp(pl.getName(), 8, false) + sp(pl.getBalance() + " €", 7, true) + "\n");
		}
		
		res.append("\n");
		
		// Circuit
		ICircuit circuit = getCircuit();
		int pas = 32;
		int i = 0;
		ICase[] cases = circuit.getCases();
		IPlayer p;
		StringBuffer numLine = null;
		StringBuffer caseLine = null;
		StringBuffer typeLine = null;
		for (ICase c : cases) {
			if (i % pas == 0) {
				numLine = new StringBuffer("");
				caseLine = new StringBuffer(" |");
				typeLine = new StringBuffer("");
			}
			if (i == circuit.getBankPosition() + 1 || i == circuit.getCityHallPosition() + 1) {
				numLine.append(" ");
				caseLine.append("|");
				typeLine.append(" ");
			}
			numLine.append("  " + sp("" + i, 2, true));
			p = circuit.getPlayer(c);
			caseLine.append(" " + (p == null ? "_" : p.getName().charAt(0)) + " ");
			caseLine.append("|");
			typeLine.append("   ");
			switch (c.getState()) {
			case BUY: typeLine.append("$"); break;
			case BUILDING_PERMIT: typeLine.append("#"); break;
			case FREE_BUILDING: typeLine.append("B"); break;
			case FREE_ENTRY: typeLine.append("E"); break;
			}
			++i;
			if (i % pas == 0) {
				res.append(numLine + "\n");
				res.append(caseLine + "\n");
				res.append(typeLine + "\n");
			}
		}
		if (i % pas != 0) {
			res.append(numLine + "\n");
			res.append(caseLine + "\n");
			res.append(typeLine + "\n");
		}
		
		res.append("\n");
		
		// Hôtels
		IHotelsArea hotels = getHotelsArea();
		int j;
		for (IProperty prop : hotels.getProperties()) {
			p = getOwner(prop);
			res.append("[" + (p == null ? " " : "" + p.getName().charAt(0)) + "]");
			res.append(sp(prop.getName(), 15, false));
			for (j = 0; j < hotels.getBuiltNb(prop); ++j) {
				res.append(" █");
			}
			while (j < prop.getCapacity()) {
				res.append(" ─");
				++j;
			}
			if (hotels.hasFunArea(prop)) {
				res.append(" _+_+_");
			} else {
				res.append(" _____");
			}
			res.append("\n");
		}
		
		return res.toString();
	}
	
	// OUTILS
	
	private String sp(String s, int min, boolean before) {
		StringBuffer sp = new StringBuffer();
		for (int i = s.length(); i < min; ++i) {
			sp.append(" ");
		}
		return before ? sp + s : s + sp;
	}

}
